package com.wonders.xianningsocialinsurance.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wonders.xianningsocialinsurance.R;

public class MenuFragment extends Fragment {
    private WebView webView;
    private String url;
    public static MenuFragment newInstance(String url){
        MenuFragment menuFragment = new MenuFragment();
        // Required empty public constructor
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        menuFragment.setArguments(bundle);
        return menuFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getArguments().getString("url");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return initView(inflater.inflate(R.layout.fragment_menu, container, false));
    }

    private View initView(View rootView){
        webView = (WebView) rootView.findViewById(R.id.webView);
        webView.loadUrl(url);
        webView.setDrawingCacheEnabled(true);
        WebSettings wSet = webView.getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setJavaScriptCanOpenWindowsAutomatically(true);
        // 设置可以支持缩放
        wSet.setSupportZoom(false);
        // 设置出现缩放工具
        wSet.setBuiltInZoomControls(false);
        //扩大比例的缩放
        wSet.setUseWideViewPort(true);
        //自适应屏幕
        wSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) { // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }
        });
        return rootView;
    }
}
